# -*- coding:utf-8 -*-
import time
from Public_way.AutoTestLog import Logger
import datetime
from selenium.webdriver.support.ui import WebDriverWait


class AddCaseP:

    def __init__(self, driver):
        self.driver = driver

    Logger.initLogger('AutoTest'+ '_Patient_way_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S'))




    def Add_Case(self, Dis):
        logger2 = Logger()
        logger2.log(u'患者端-进行上传病历操作')
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        time.sleep(2)
        # # 选择就诊人
        # WebDriverWait(self.driver, 500).until(
        #     lambda x: x.find_by_id('com.jkgj.skymonkey.patient:id/patient_name_rl')).click()
        #
        # WebDriverWait(self.driver, 500).until(
        #     lambda x: x.find_by_id('com.jkgj.skymonkey.patient:id/patient_tel_number')).click()
        # 添加病症描述
        WebDriverWait(self.driver, 10).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_illness_one')).send_keys(Dis)
        # 提交病历
        WebDriverWait(self.driver, 10).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/back_next')).click()
        logger2.log(u'患者端-病历添加成功')

    def LookDoctor(self):
        logger2 = Logger()
        logger2.log(u'患者端-马上进入直面专家主流程')
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        time.sleep(2)
        self.driver.tap([(528, 993)], 500)
        # 直面专家
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/video_rl')).click()
        logger2.log(u'患者端-点击直面专家按钮')
        # 选择专家
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/middle_rl')).click()
        logger2.log(u'患者端-选择一个专家成功')
        # 开始预约 创建订单
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/bottom_video_rl')).click()
        logger2.log(u'患者端-开始预约专家')
        # 确认预约
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_know_confirm')).click()
        logger2.log(u'患者端-预约专家成功，订单创建成功')
        # 收取红包
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/i_know')).click()
        logger2.log(u'患者端-活动流程完成，收取红包-等待患者付款')

    def PayVideo(self, Code, message):
        logger2 = Logger()
        # 去付款
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/root_to_pay')).click()
        logger2.log(u'患者端-用户点击去付款，去支付方式页面')
        # 选择9k钱包
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/iv_bank_tag')).click()
        logger2.log(u'患者端-支付方式选择9k钱包')
        # 预约支付
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_ensure_pay')).click()
        logger2.log(u'患者端-点击预约支付按钮-设置新的支付密码')
        # 设置支付密码
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_positive')).click()
        logger2.log(u'患者端-进入设置新的支付密码流程')
        # 输入验证码
        self.Ucode(Code)
        logger2.log(u'患者端-输入验证码')
        self.Ucode(Code)
        logger2.log(u'患者端-输入支付密码成功')
        self.Ucode(Code)
        logger2.log(u'患者端-再次输入支付密码成功')
        # 密码设置完成-提交
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_set_pay_pwd_submit')).click()
        logger2.log(u'患者端-支付密码设置完成')
        # 预约支付
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_ensure_pay')).click()
        # 立即支付
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_pay_now_submit')).click()
        logger2.log(u'患者端-开始付款')
        # 输入支付密码
        self.Pcode(Code)
        logger2.log(u'患者端-支付密码输入完成')
        number = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_location'))
        logger2.log(u'患者端-预约成功-付款成功-目前排位为 %s' % number.text.encode('utf-8'))
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/i_know')).click()
        WebDriverWait(self.driver, 600).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/i_know')).click()
        # 患者点击接听视频通话
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/open_video')).click()
        time.sleep(3)
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_renew')).click()
        logger2.log(u'患者端-患者点击续约')
        # 选择9k钱包
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/iv_bank_tag')).click()
        logger2.log(u'选患者端-择9k钱包')
        # 预约支付
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_ensure_pay')).click()
        # 立即支付
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_pay_now_submit')).click()
        # 输入支付密码
        self.Pcode(Code)
        logger2.log(u'患者端-输入支付密码')
        logger2.log(u'患者端-续约成功')
        # 发送消息s
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_message')).click()
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/et_sendmessage')).send_keys(message)
        logger2.log(u'患者端-患者发送IM消息s')
        # 点击发送
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/btn_send')).click()
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/chat_title')).click()
        logger2.log(u'患者端-点击发送')
        logger2.log(u'患者端-等待患者端关闭视频，大概10秒钟后关闭视频')
        time.sleep(10)
        # 关闭视频
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_down')).click()
        videoTime = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_chatcontent')).text.encode('utf-8')
        # 订单详情
        time.sleep(5)
        WebDriverWait(self.driver, 100).until(
            lambda x: x.find_element_by_name('订单详情')).click()
        logger2.log(u'患者端-点击订单详情按钮')
        # 开始时间
        StartTime = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_start_time'))
        # 结束时间
        EndTime = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_end_time'))
        # 退款
        RefundMoney = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_surp_price'))
        # 总时长
        AllTime = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_appointment_total_time'))
        # 剩余时长
        AlsoTime = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_surp_time'))
        logger2.log('患者端-结束时间 % s' % EndTime)
        logger2.log('患者端-退款 % s' % RefundMoney)
        logger2.log('患者端-总时长 % s' % AllTime)
        logger2.log('患者端-剩余时长 % s' % AlsoTime)
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        # 订单状态
        time.sleep(3)
        OrderState = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/consult_status'))
        logger2.log('患者端-订单状态 % s' % OrderState.text.encode('utf-8'))
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        # 查看当前剩余余额
        tv_balance_num = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_balance_num'))
        logger2.log('患者-视频后余额 % s' % tv_balance_num.text.encode('utf-8'))

    def Expert_ref(self):
        # 查看专家拒诊后发来的消息
        dis = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_chatcontent'))
        print '专家拒诊的消息是 %s ' % dis.text.encode('utf-8')

    # 专家发起视频后，患者取消
    def P_ref(self):
        # 患者点击取消视频通话
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/cancle_video')).click()
        # 患者查看退款详情
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_chatcontent')).click()
        balance = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_balance_recharge_text'))
        print '退款余额为 %s' % balance.text.encode('utf-8')
        # 返回到首页
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        # 咨询记录页面，查看预约状态
        state = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/consult_status'))
        print '当前预约状态为 %s ' % state.text.encode('utf-8')
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
        # 查看患者余额
        money = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_balance_num'))
        print '患者余额为 %s' % money.text.encode('utf-8')

    # 专家发起视频后，患者接听视频
    def Start_P_Video(self):
        # 患者点击接听视频通话
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/open_video')).click()

    def LookDocteor(self, Code, message):
        logger = Logger()
        # 查看当前剩余余额
        tv_balance_num = WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_balance_num'))
        logger.log('患者-查看当前剩余余额' + tv_balance_num.text.encode('utf-8'))
        # 点击个人中心的我的医生
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_my_attention')).click()
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/linearlayout_info')).click()
        logger.log('点击个人中心的我的医生')
        # 预约专家
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/bottom_video_rl')).click()
        logger.log(u'预约专家')
        # 选择病历
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/radio_rl')).click()
        logger.log(u'选择病历')
        # 点击直面专家
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/video_rl')).click()
        logger.log(u'点击直面专家')
        # 预约专家
        WebDriverWait(self.driver, 60).until(
            lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_know_confirm')).click()
        # 判断判断是否重复预约
        try:
                a = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_positive'))
                logger.log(u'目前重复预约专家，现在取消预约')
                WebDriverWait(self.driver, 60).until(
                    lambda x: x.find_element_by_id("com.jkgj.skymonkey.patient:id/tv_positive")).click()
                # 点击条目
                WebDriverWait(self.driver, 60).until(
                    lambda x: x.find_element_by_id("com.jkgj.skymonkey.patient:id/server_statu_tv")).click()
                # 取消预约
                WebDriverWait(self.driver, 60).until(
                    lambda x: x.find_element_by_id("com.jkgj.skymonkey.patient:id/cancle_order1")).click()
                WebDriverWait(self.driver, 60).until(
                    lambda x: x.find_element_by_name("取消预约")).click()
                WebDriverWait(self.driver, 60).until(
                    lambda x: x.find_element_by_id("com.jkgj.skymonkey.patient:id/rl_back")).click()
                logger.log(u'取消预约成功')
                self.driver.quit()
        except:
                pass
        finally:
            # 去付款
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/root_to_pay')).click()
            # 选择9k钱包
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/iv_bank_tag')).click()
            # 预约支付
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_ensure_pay')).click()
            # 立即支付
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_pay_now_submit')).click()
            # 输入支付密码
            self.Pcode(Code)
            number = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_location'))
            logger.log('预约成功-付款成功-目前排位为 %s' % number.text.encode('utf-8'))
            # 患者点击接听视频通话
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/open_video')).click()
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_renew')).click()
            logger.log(u'患者续约')
            # 选择9k钱包
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/iv_bank_tag')).click()
            logger.log(u'选择9k钱包')
            # 预约支付
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_ensure_pay')).click()
            # 立即支付
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_pay_now_submit')).click()
            # 输入支付密码
            self.Pcode(Code)
            logger.log(u'输入支付密码')
            logger.log(u'续约成功')
            # 发送消息s
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_message')).click()
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/et_sendmessage')).send_keys(message)
            logger.log(u'患者发送IM消息s')
            # 点击发送
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/btn_send')).click()
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/chat_title')).click()
            logger.log(u'点击发送')
            logger.log(u'等待患者端关闭视频')
            time.sleep(10)

            # 关闭视频
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/menu_down')).click()
            videoTime = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_chatcontent')).text.encode('utf-8')
            logger.log('总预约时间为 % s' % videoTime)
            # 订单详情
            time.sleep(5)
            WebDriverWait(self.driver, 100).until(
                lambda x: x.find_element_by_name('订单详情')).click()
            logger.log(u'点击订单详情按钮')
            # 开始时间
            StartTime = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_start_time'))
            # 结束时间
            EndTime = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_end_time'))
            # 退款
            RefundMoney = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_surp_price'))
            # 总时长
            AllTime = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_appointment_total_time'))
            # 剩余时长
            AlsoTime = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_surp_time'))
            logger.log('结束时间 % s' % EndTime)
            logger.log('退款 % s' % RefundMoney)
            logger.log('总时长 % s' % AllTime)
            logger.log('剩余时长 % s' % AlsoTime)
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
            # 订单状态
            time.sleep(3)
            OrderState = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/consult_status'))
            logger.log('订单状态 % s' % OrderState.text.encode('utf-8'))
            WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/rl_back')).click()
            # 查看当前剩余余额
            tv_balance_num = WebDriverWait(self.driver, 60).until(
                lambda x: x.find_element_by_id('com.jkgj.skymonkey.patient:id/tv_balance_num'))
            logger.log('患者-视频后余额 % s' % tv_balance_num.text.encode('utf-8'))


    def Ucode(self, code):
        time.sleep(5)
        # 输入验证码
        for i in code:
            if i == '0':
                self.driver.tap([(537, 1847)], 500)
            elif i == '1':
                self.driver.tap([(186, 1362)], 500)
            elif i == '2':
                self.driver.tap([(537, 1362)], 500)
            elif i == '3':
                self.driver.tap([(903, 1362)], 500)
            elif i == '4':
                self.driver.tap([(182, 1515)], 500)
            elif i == '5':
                self.driver.tap([(541, 1515)], 500)
            elif i == '6':
                self.driver.tap([(897, 1515)], 500)
            elif i == '7':
                self.driver.tap([(180, 1676)], 500)
            elif i == '8':
                self.driver.tap([(549, 1676)], 500)
            elif i == '9':
                self.driver.tap([(906, 1676)], 500)

    def Pcode(self, Code):
        time.sleep(5)
        # 输入验证码
        for i in Code:
            if i == '6':
                self.driver.tap([(895, 1380)], 500)
            elif i == '1':
                self.driver.tap([(186, 1179)], 500)
            elif i == '2':
                self.driver.tap([(537, 1183)], 500)
            elif i == '3':
                self.driver.tap([(903, 1183)], 500)
            elif i == '4':
                self.driver.tap([(182, 1391)], 500)
            elif i == '5':
                self.driver.tap([(541, 1391)], 500)
            elif i == '0':
                self.driver.tap([(530, 1802)], 500)
            elif i == '7':
                self.driver.tap([(180, 1603)], 500)
            elif i == '8':
                self.driver.tap([(549, 1603)], 500)
            elif i == '9':
                self.driver.tap([(906, 1603)], 500)
